// pages/grow_log/grow_log.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')
import * as echarts from '../../ec-canvas/echarts';


Page({

  /**
   * 页面的初始数据
   */
  data: {
    host:app.globalData.host,
    baby:true,
    login:true,
    ec: {
      // 将 lazyLoad 设为 true 后，需要手动初始化图表
      lazyLoad: true
    },
    TabCur: 0,
    scrollLeft:0,
    type:1,
    modalName:'',
    tabList:[
      {
        name:'记录列表',
        type:1,
      },
      {
        name:'成长自测',
        type:2,
      },
      {
        name:'生长发育曲线图',
        type:3,
      },
    ]
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id-1)*60
    })
    this.setData({
      type:this.data.tabList[this.data.TabCur].type
    })
    if(this.data.type == 1){
      this.getMsgList(1,'');
    }else if(this.data.type == 3){
      this.height();
    }else if(this.data.type == 2){
      // this.weight();
      wx.navigateTo({
        url: '/pages/test_cate/test_cate',
      })
    }
  },
  showModal:function(e){
    this.setData({
      modalName: 'modal1',
      delete_id:e.currentTarget.dataset.delete_id
    })
  },
  hideModal:function(){
    this.setData({
      modalName: '',
    })
  },

  /**
   * 删除
   * @param {*} e
   */
  delete:function(e){
    var that = this;
    util.request('baby_grow_log/delete', {id:that.data.delete_id}, function (data) {
      that.setData({
        delete_id: '',
        modalName:'',
      })
      if(data.code == 1){
        that.getMsgList(1,'');
      }else{
        util.fail(data.msg,that);
      }
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    this.ecComponent = this.selectComponent('#mychart-dom-line');

  },
  setOption:function(chart) {
    var that = this;
    const option = {
      color: ['#e53c6c'],
      grid: {
        containLabel: true
      },
      tooltip: {
        show: false,
        trigger: 'axis',
        
      },
      xAxis: {
        type: 'category',
        name: "食用周期(月)",
        nameLocation: "middle",
        nameTextStyle: {
          lineHeight: 40,
        },
        splitLine: {
          show: true
        },
        axisTick: {
          show: false
        },
        // minorTick: {
        //   splitNumber: 10
        // },
        axisLabel:{
          align: "right",
          interval(index,value){
            console.log(index,value);
            if(value.indexOf(".") == -1){
              return true;
            }
          }
        },
        // axisLabel: {
        //   align: "left"
        // },
        data: that.data.canvas_data.x,
      },
      yAxis: {
        // x: 'center',
        type: 'value',
        name: that.data.type==3?"身高（cm）":"体重(kg)",
        interval:30,
        max:210,
      },
      series: [
      {
        name: 'baby',
        type: 'line',
        connectNulls:true,
        // itemStyle : { normal: {label : {show: true}}},
        smooth: true,
        data:that.data.canvas_data.y,
      }]
    };
  
    chart.setOption(option);
  },

  init: function () {
    // var that = this;
    this.ecComponent.init((canvas, width, height, dpr) => {
      // 获取组件的 canvas、width、height 后的回调函数
      // 在这里初始化图表
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // new
      });
      this.setOption(chart);

      // 将图表实例绑定到 this 上，可以在其他成员函数（如 dispose）中访问
      this.chart = chart;

      this.setData({
        isLoaded: true,
        isDisposed: false
      });

      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return chart;
    });
  },
  detail:function(){
    var that = this;
    util.request('baby/detail', {baby_id:app.globalData.parent.baby_default_id}, function (data) {
      console.log(data.data.baby)
      if(data.code == 1){
          that.setData({
            child:data.data.baby,
          });
      }else{
        util.fail(data.msg,that);
      }
    });
  },


  weight:function(){
    var that = this;
    util.request('baby_grow_log/weight', {baby_id:app.globalData.parent.baby_default_id}, function (data) {
      console.log(data.data)
      if(data.code == 1){
          that.setData({
            canvas_data:data.data,
          });
          console.log(that.data.canvas_data)
          that.init();
      }else{
        util.fail(data.msg,that);
      }
    });
  },

  height:function(){
    var that = this;
    util.request('baby_grow_log/eatLog', {baby_id:app.globalData.parent.baby_default_id}, function (data) {
      console.log(data.data)

      if(data.code == 1){
          that.setData({
            canvas_data:data.data,
          });
          console.log(that.data.canvas_data)
          that.init();
      }else{
        util.fail(data.msg,that);
      }
    });
  },
  onShow:function(){
    if(!app.globalData.login){
      this.setData({
        login:false,
      })
      // wx.redirectTo({
      //   url: '/pages/site/login/login',
      // })
    }else if(!app.globalData.parent.baby_default_id){
      this.setData({
        baby:false,
      })
      // wx.redirectTo({
      //   url: '/pages/baby/add/add',
      // })
    }else{
      this.setData({
        baby:true,
        login:true
      })
      this.detail();
      if(this.data.type == 1){
        this.getMsgList(1,'');
      }else if(this.data.type == 2){
        this.setData({
          TabCur: 0,
          scrollLeft:0
        })
        this.setData({
          type:1
        })
        this.getMsgList(1,'');
        // this.height();
        // wx.redirectTo({
        //   url: '/pages/test_cate/test_cate',
        // })
      }else if(this.data.type == 3){
        console.log(33333333333)
        // this.weight();
        this.height();
      }
    }

  },

  onPullDownRefresh: function () {
    if (!this.loading) {
      wx.showNavigationBarLoading();
      this.getMsgList(1,'top');
    }
  },

  onReachBottom: function () {
    var that = this;
    if (!this.data.loading && this.data.page < this.data.pages) {
      that.setData({
        isHideLoadMore: false,
        page: this.data.page+1
      });
      this.getMsgList(that.data.page, 'bottom');
    }
  },

  getMsgList: function(pageNo,pos) {
    var url = 'baby_grow_log/index',param = {baby_id:app.globalData.parent.baby_default_id};
    param.pageNo = pageNo;
    util.pageList(this,url,param, pos);
  },
})
